package com.sduept.nwld.dataserver.manager.message;

import com.esms.MessageData;
import com.esms.PostMsg;
import com.esms.common.entity.Account;
import com.esms.common.entity.GsmsResponse;
import com.esms.common.entity.MTPack;
import com.esms.common.entity.MTPack.MsgType;
import com.esms.common.util.Interceptor;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Mos短信平台消息发送的方法
 * @author     Xiaopy
 * @version    V1.0
 * @Date       2018年9月21日 上午10:43:29
 */
@Service
@Slf4j
public class MosMessagePushManager {
	
	private static final int BIZTYPE=1;
	private static final int MAX_SIZE=10000;
	
	@Autowired
	private ParamManager paramCache;
	

	private Account account;
	
	private PostMsg pm;
	
	
	
	/**
	 * 设置相关参数
	 * @param pm
	 * @param account
	 * @return
	 */
	private boolean setParams(){
		if(!isAvailable()){
			return false;
		}
		List<Object> list = getparams();
		if(list.size()!=6){
			return false;
		}
		pm = new PostMsg(false, 2000, 1);
		pm.setInterceptor(new Interceptor() {
			public void beforeMtSend(long waitConnTime, long loginTime, MTPack pack) {
				//加上自定义的日志输出
				log.info("MT before send [" + pack.getBatchID() + "] wait:" + waitConnTime + ", login:" + loginTime + ", now:" + System.currentTimeMillis());
			}
		});
		//设置网关的IP和port，用于发送信息
		pm.getCmHost().setHost((String)list.get(0), (int)list.get(1));
		//设置网关的 IP和port，用于获取账号信息、上行、状态报告等等
		pm.getWsHost().setHost((String)list.get(2), (int)list.get(3));
		account = new Account((String)list.get(4), (String)list.get(5));
		return true;
	}
	/**
	 * 组发消息接口
	 * @param msgList
	 * @param batchName
	 * @param Content
	 * @throws Exception
	 */
	public void sendMsgByGroup(List<MessageData> msgList,String batchName) throws Exception{
		if(!setParams()){
			return ;
		}
		int count=(int) Math.ceil((double)msgList.size()/MAX_SIZE);
		for(int i=0;i<count;i++){
			if(i==count-1){
				List<MessageData> tmp = msgList.subList(i*MAX_SIZE,msgList.size());
				doSendMsgByGroup(pm,account,tmp,batchName);
			}else{
				List<MessageData> tmp = msgList.subList(i*MAX_SIZE, i*MAX_SIZE+MAX_SIZE);
				doSendMsgByGroup(pm,account,tmp,batchName);
			}
		}
	}
	
	/**
	 * 群发消息接口
	 * @param phones
	 * @param batchName
	 * @param Content
	 * @throws Exception
	 */
	public void sendMsgByMass(List<String> phones,String batchName,String Content) throws Exception{
		if(!setParams()){
			return ;
		}
		int count=(int)Math.ceil((double)phones.size()/MAX_SIZE);
		for(int i=0;i<count;i++){
			if(i==count-1){
				List<String> tmp = phones.subList(i*MAX_SIZE,phones.size());
				doSendMsgByMass(pm,account,tmp,batchName,Content);
			}else{
				List<String> tmp = phones.subList(i*MAX_SIZE, i*MAX_SIZE+MAX_SIZE);
				doSendMsgByMass(pm,account,tmp,batchName,Content);
			}
		}
		
	}
	
	/**
	 * 判断mos短信平台是否启用
	 * @return
	 */
	private boolean isAvailable(){
		SystemParameter systemParameter = paramCache.get(ParamDesc.MOS_SERVICE_STATUS);
		if(null==systemParameter||StringUtils.isEmpty(systemParameter.getValue())){
			return false;
		}
		int satus=0;
		try{
			satus = Integer.parseInt(systemParameter.getValue());
		}catch(Exception e){
			log.error("数字解析异常");
		}
		if(0==satus){
			return false;
		}
		return true;
	}
	
	/**
	 * 拿到系统参数，顺序确定
	 * @return
	 */
	private List<Object> getparams(){
		List<Object> result=new ArrayList<>();
		SystemParameter systemParameter1 = paramCache.get(ParamDesc.MOS_PARAM_MESSAGE_ADDRESS);
		SystemParameter systemParameter2 = paramCache.get(ParamDesc.MOS_PARAM_MESSAGE_PORT);
		SystemParameter systemParameter3 = paramCache.get(ParamDesc.MOS_PARAM_OTHER_ADDRESS);
		SystemParameter systemParameter4 = paramCache.get(ParamDesc.MOS_PARAM_OTHER_PORT);
		SystemParameter systemParameter5 = paramCache.get(ParamDesc.MOS_PARAM_ACCOUNT_USERNAME);
		SystemParameter systemParameter6 = paramCache.get(ParamDesc.MOS_PARAM_ACCOUNT_PASSWORD);
		if(null==systemParameter1||null==systemParameter2||null==systemParameter3||
				null==systemParameter4||null==systemParameter5||null==systemParameter5){
			return  result;
		}
		if(StringUtils.isEmpty(systemParameter1.getValue())||StringUtils.isEmpty(systemParameter2.getValue())||StringUtils.isEmpty(systemParameter3.getValue())
				||StringUtils.isEmpty(systemParameter4.getValue())||StringUtils.isEmpty(systemParameter5.getValue())||StringUtils.isEmpty(systemParameter6.getValue())){
			return  result;
		}
		try{
			result.add(systemParameter1.getValue());
			result.add(Integer.parseInt(systemParameter2.getValue()));
			result.add(systemParameter3.getValue());
			result.add(Integer.parseInt(systemParameter4.getValue()));
			result.add(systemParameter5.getValue());
			result.add(systemParameter6.getValue());
		}catch(Exception e){
			e.printStackTrace();
		}
		return  result;
	}
	/**
	 * 短信消息群发方法
	 * @param postMsg
	 * @param account
	 * @param phones
	 * @param Content
	 * @throws Exception 
	 */
	private void doSendMsgByMass(PostMsg postMsg,Account account,List<String> phones,String batchName,String Content) throws Exception{
		MTPack pack = new MTPack();
		pack.setBatchID(UUID.randomUUID());
		pack.setBatchName(batchName);
		pack.setMsgType(MsgType.SMS);
		pack.setBizType(BIZTYPE);
		pack.setDistinctFlag(false);
		pack.setSendType(MTPack.SendType.MASS);
		List<MessageData> msglist = new ArrayList<MessageData>();
		for(String str:phones){
			msglist.add(new MessageData(str,Content));
		}
		pack.setMsgs(msglist);
		GsmsResponse resp = postMsg.post(account, pack);
		if(resp!=null){
			log.info(resp.toString());
		}

	}
	
	/**
	 * 短信消息组发方法
	 * @param postMsg
	 * @param account
	 * @param batchName
	 * @param contentMap
	 * @throws Exception
	 */
	private void doSendMsgByGroup(PostMsg postMsg,Account account,List<MessageData> msglist,String batchName) throws Exception{
		MTPack pack = new MTPack();
		pack.setBatchID(UUID.randomUUID());
		pack.setBatchName(batchName);
		pack.setMsgType(MsgType.SMS);
		pack.setBizType(BIZTYPE);
		pack.setDistinctFlag(false);
		pack.setSendType(MTPack.SendType.GROUP);
		pack.setMsgs(msglist);
		GsmsResponse resp = postMsg.post(account, pack);
		if(resp!=null){
			log.info(resp.toString());
		}

	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}

	
}
